package com.wongoing.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

/**
 * 功能说明：继承了Shiro的单元测试基类，单元测试并不是真正的web环境，因此把使用的是spring-shiro-test.xml，其中SecurityManager使用的是DefaultSecurityManager，而不是DefaultWebSecurityManager
 * 修改说明：
 * @author zhenglibing
 * @date 2017年10月14日 下午9:31:29
 * @version V0.1
 */
@RunWith(SpringJUnit4ClassRunner.class)		//告诉spring怎样执行
@WebAppConfiguration		//标明是web应用测试
@ContextConfiguration(locations={"classpath:spring.xml", "classpath:spring-mvc.xml", "classpath:spring-shiro-test.xml"})		//load配置文件
public class MyShiroActionTestCase<T> {
//	private static ThreadState subjectThreadState;
	private DefaultSecurityManager securityManager;
	protected T action = null;
	private MockMvc mockMvc;
	private MockHttpSession session;
	
	@Autowired
	public void setSecurityManager(DefaultSecurityManager securityManager) {
		this.securityManager = securityManager;
		SecurityUtils.setSecurityManager(this.securityManager);
	}
	
	@Autowired
	protected void setAction(T action) {
		this.action = action;
		this.mockMvc = MockMvcBuilders.standaloneSetup(this.action).build();
		this.session = new MockHttpSession();
	}
	
	protected MockMvc getMockMvc() {
		return this.mockMvc;
	}
	
	protected MockHttpSession getSession() {
		return this.session;
	}
	
}
